﻿@model PagingDataSet<SpecialContentItem>
@{
    long firstContentItemId = ViewData.Get<long>("firstContentItemId", 0);
    long lastContentItemId = ViewData.Get<long>("lastContentItemId", 0);
    var typeId = ViewData.Get<int>("typeId", 0);
    var belong = ViewData.Get<string>("belong", string.Empty);
    Layout = null;

}
@if (Model.Count() > 0)
{
    <table class="table tn-va-middle">
        <thead>
            <tr>
                <th>内容标题</th>
                <th>推荐类别</th>
                <th>所属</th>
                <th>推荐人</th>
                <th>推荐时间</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var specialContent in Model)
            {
            <tr>
                <td>
                    @if (specialContent.Link == null || specialContent.Link == string.Empty)
                    {
                        if (specialContent.ItemName.Length > 30)
                        {
                        <a href="@specialContent.GetCommentedObjectUrl()" title="@specialContent.ItemName" target="_blank"> @specialContent.ItemName.Substring(0, 30)...</a>
                        }
                        else
                        {
                        <a href="@specialContent.GetCommentedObjectUrl()" target="_blank"> @specialContent.ItemName</a>
                        }
                    }
                    else
                    {
                        if (specialContent.ItemName.Length > 30)
                        {
                        <a href="@specialContent.Link" title="@specialContent.ItemName" target="_blank"> @specialContent.ItemName</a>
                        }
                        else
                        {
                        <a href="@specialContent.Link" title="@specialContent.ItemName" target="_blank"> @specialContent.ItemName</a>
                        }
                    }
                </td>
                <td>
                    @{
                        var specialContentType = specialContent.GetSpecialContentType();
                        <text> @(specialContentType != null ? specialContentType.Name : string.Empty    )</text>

                    }
                </td>
                <td>
                    @{
                        var specialContentTenantType = specialContent.GetSpecialContentTenantType();
                        <text> @(specialContentTenantType != null ? specialContentTenantType.Name : string.Empty    )                    </text>

                    }
                </td>
                <td>
                    @specialContent.Recommender
                </td>
                <td>
                    @specialContent.DateCreated.ToUserDateString()
                </td>
                <td class="text-nowrap operation" data-id="@specialContent.Id">
                    <a class="a tn-mr-10 layerrecommendcontent" data-href="@SiteUrls.Instance()._RecommendContent(specialContent.ItemId,specialContent.TenantTypeId,specialContent.ItemName,specialContentItemId:specialContent.Id)', '450" data-id="@specialContent.ItemId" data-tenanttypeid="@specialContent.TenantTypeId" data-subject="@specialContent.ItemName" data-specialContentid="@specialContent.Id" title="编辑" href="javascript:;"><i class="fa fa-pencil"></i></a>
                    <a class="a tn-mr-10 deleteItem" title="删除" href="javascript:;" data-id="@specialContent.Id"><i class="fa fa-trash"></i></a>
                    @if ((firstContentItemId != 0 && specialContent.Id == firstContentItemId) || (string.IsNullOrEmpty(belong)))
                    {
                    }
                    else
                    {
                    <a class="a tn-mr-10 changeorder-up" href="javascript:;" data-id="@specialContent.Id"><i class="fa fa-arrow-up"></i></a>
                    }
                    @if ((lastContentItemId != 0 && specialContent.Id == lastContentItemId) || (string.IsNullOrEmpty(belong)))
                    {
                    }
                    else
                    {
                    <a class="a tn-mr-10 changeorder-down" href="javascript:;" data-id="@specialContent.Id"><i class="fa fa-arrow-down"></i></a>
                    }
                </td>
            </tr>
                        }
        </tbody>
    </table>
                        }
                        else
                        {
                        <div class="text-center tn-mb-20 tn-mt-20">
                            该类别暂无推荐内容
                        </div>
                        }
<div class="text-center">
    @Html.AjaxPagingButton(Model, "_listSpecialContentItems", CachedUrlHelper.Action("_ListSpecialContentItems", "ControlPanel", "", new RouteValueDictionary(new { belong = belong, typeId = typeId })))
</div>
@Styles.Render("~/Bundle/Styles/Uploader")
<script>
    require(['jquery', 'page', 'tnlayer', 'uploader'], function ($, _, tnlayer) {
        $('.layerrecommendcontent').click(function () {
            var id = $(this).data("id");
            var tenanttypeId = $(this).data("tenanttypeid");
            var subject = $(this).data("subject");
            var specialContentId = $(this).data("specialContentid");
            tnlayer.layermodal('内容推荐编辑', '@CachedUrlHelper.Action("_RecommendContent", "Common")?itemId=' + id + '&tenantTypeId=' + tenanttypeId + '&title=' +encodeURI( subject) + '&specialContentItemId=' + specialContentId, '450px', 'auto')
        })
        $(function () {
            //删除推荐内容
            $(".deleteItem").click(function () {
                var id = $(this).data("id");
                var type = $("#specialContentType option:selected").val();
                var belong = $("#contentTenant option:selected").val();
                layer.confirm("是否确认取消推荐该内容", {
                    btn: ['确认', '取消']
                },
                function () {
                    $.post('@CachedUrlHelper.Action("_DeleteSpecialContentItem", "ControlPanel")', { specialContentItemId: id },
                        function (data) {
                            if (data.state = 1) {
                                layer.msg("取消成功", { icon: 1 });
                            }
                            else {
                                layer.msg("取消失败", { icon: 2 });
                            }
                            $.get("@CachedUrlHelper.Action("_ListSpecialContentItems", "ControlPanel")", { belong: belong, typeId: type, pageIndex: "@Model.PageIndex", requestTime: new Date().getTime() }, function (data) {
                                $("#_listSpecialContentItems").html(data);
                            })
                        })
                })
            })
            //向上调整内容顺序
            $(".changeorder-up").click(function () {
                var id = $(this).data("id");
                var previouId;
                var operations = $(".operation");
                var type = $("#specialContentType option:selected").val();
                var belong = $("#contentTenant option:selected").val();
                for (var i = 0; i < operations.length; i++) {
                    if ($(operations[i]).data("id") == id) {
                        if (i == 0) {
                            previouId = id;
                        }
                        else {
                            previouId = $(operations[i - 1]).data("id");
                        }
                    }
                }
                $.post('@CachedUrlHelper.Action("_ChangeSpecialContentOrder", "ControlPanel")', { firstId: id, secondId: previouId },
                    function (data) {
                        if (data.state == 1) {
                            $.get("@CachedUrlHelper.Action("_ListSpecialContentItems", "ControlPanel")", { belong: belong, typeId: type, pageIndex: "@Model.PageIndex", requestTime: new Date().getTime() }, function (data) {
                                $("#_listSpecialContentItems").html(data);
                            })
                        }
                    })
            })
            //向下调整内容顺序
            $(".changeorder-down").click(function () {
                var id = $(this).data("id");
                var nextId;
                var operations = $(".operation");
                var type = $("#specialContentType option:selected").val();
                var belong = $("#contentTenant option:selected").val();
                for (var i = 0; i < operations.length; i++) {
                    if ($(operations[i]).data("id") == id) {
                        if (i == operations.length) {
                            nextId = id;
                        }
                        else {
                            nextId = $(operations[i + 1]).data("id");
                        }
                    }
                }
                $.post('@CachedUrlHelper.Action("_ChangeSpecialContentOrder", "ControlPanel")', { firstId: id, secondId: nextId }, function (data) {
                    if (data.state == 1) {
                        $.get("@CachedUrlHelper.Action("_ListSpecialContentItems", "ControlPanel")", { belong: belong, typeId: type, pageIndex: "@Model.PageIndex", requestTime: new Date().getTime() }, function (data) {
                            $("#_listSpecialContentItems").html(data);
                        })
                    }
                })
            })
        })
        //推荐成功回调
        $.fn.recommendCallBack = function (data) {
            var type = $("#specialContentType option:selected").val();
            var belong = $("#contentTenant option:selected").val();
            if (data.state == 1) {
                layer.closeAll();
                layer.msg("编辑推荐成功", { icon: 1 });
                $.get("@CachedUrlHelper.Action("_ListSpecialContentItems", "ControlPanel")", { belong: belong, typeId: type, pageIndex: "@Model.PageIndex", requestTime: new Date().getTime() }, function (data) {
                    $("#_listSpecialContentItems").html(data);
                })
            }
            else {
                layer.msg("编辑推荐失败", { icon: 2 });
            }
        }
    })
</script>